﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
namespace BuddhistSchools.DTO
{
   public class XMLObject
    {
       const string defaultData = "<?xml version='1.0' encoding='utf-8'?>";
       private XmlSerializer _XmlSerializer;
       public XmlSerializer XmlSer
       {
           get { return _XmlSerializer; }
           set { _XmlSerializer = value; }
       }
       public XMLObject()
       {
           XmlSer = new XmlSerializer(typeof(object));
       }
       public XMLObject(XmlSerializer xmlSerializer)
       {
           XmlSer = xmlSerializer;
       }
       public bool WriteXML(object dataXML, string path)
       {
           try
           {
            if (File.Exists(path)) File.Delete(path);
            StreamWriter wr = new StreamWriter(path);
            XmlSer.Serialize(wr, dataXML);
            wr.Close();
            return true;
           }
           catch
           {
               return false;
           }
          
       }
       public bool ReadXML(ref object dataXML, string path)
       {
           try
           {
               if (!File.Exists(path))File.WriteAllText(path, defaultData);
                   StreamReader rr = new StreamReader(path);
                   dataXML = XmlSer.Deserialize(rr);
                   rr.Close();
                   return true;
           }
           catch 
           {
               return false;
              
           }
       }
   }
}
